const passport = require('koa-passport');
// const LocalStrategy = require('passport-local').Strategy;
const LocalStrategy = require('passport-local');

const model = require('../model'); 

// 用户名密码验证策略
passport.use(new LocalStrategy(
  function (username, password, done) {
    model.user.getUserByNameAndPass(username, password).then(
      user => done(null, user),
      err => done(null, false, err)
    );
  }
));

// serializeUser 在用户登录验证成功以后将会把用户的数据存储到 session 中
passport.serializeUser(function(user, done) {
  done(null, user);
});

// deserializeUser 在每次请求的时候将从 session 中读取用户对象
passport.deserializeUser(function (user, done) {
  done(null, user);
});

module.exports = passport;

